// firstPackage/pages/reportModule/selectReportHouse/selectReportHouse.js
import { queryRecommendHouse } from "../../../../service/house_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    historyList: [
      {
        name: "环球融创未来城",
        houseNumber: "A1233442",
        isChoose: false
      },
      {
        name: "滨江和城",
        houseNumber: "A123398",
        isChoose: false
      },
      {
        name: "邦泰华府公馆",
        houseNumber: "A1328673",
        isChoose: false
      },
    ],
    allHouseList: [

    ],
    searchWord: "",//搜索词
    searchList: [

    ],//搜索列表
    resultHouse: {},//最终选中的房源
    formData: {
      cityName: "",
      page: 0,
      pageSize: 200,
      words: "",
    },
    realSearchList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryRecommendHouse(false);
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //获取可报备楼盘
  queryRecommendHouse(status) {
    if (status) {
      this.setData({
        realSearchList: [],
        searchList: [],
      })
    }
    wx.showLoading();
    queryRecommendHouse(this.data.formData).then(res => {
      wx.hideLoading();
      console.log("JJLog:查询到的楼盘是:" + JSON.stringify(res));
      if (res.code == 200) {
        if (res.data && res.data.length > 0) {
          if (!status) {
            let arr = res.data;
            arr.forEach((item) => {
              item.isChoose = false
            })
            this.setData({
              allHouseList: arr
            })
          } else {
            //搜索进来的:
            this.setData({
              realSearchList: res.data
            })
            let searchData = res.data.map((res) => {
              return {
                key: this.data.searchWord,
                name: res.name
              }
            })
            this.setData({
              searchList: searchData
            })
          }

        }


      }
    }).catch(err => {
      console.error("JJLog:queryRecommendHouse:", err);
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      wx.hideLoading();
    })
  },

  //房源item点击回调
  reportHouseItemBack(e) {
    // console.log("JJLog:回调:" + JSON.stringify(e));
    let { item, key } = e.detail;
    let historyList = this.data.historyList;
    let allList = this.data.allHouseList;
    allList.forEach((aItem) => {
      aItem.isChoose = false;
      if (aItem.houseNumber == item.houseNumber) {
        aItem.isChoose = !item.isChoose
        if (aItem.isChoose) {
          this.setData({
            resultHouse: aItem
          })
        } else {
          this.setData({
            resultHouse: {}
          })
        }
      }
    })
    // historyList.forEach((hItem) => {
    //   hItem.isChoose = false;
    //   if (hItem.houseNumber == item.houseNumber) {
    //     hItem.isChoose = !item.isChoose
    //     if (hItem.isChoose) {
    //       this.setData({
    //         resultHouse: hItem
    //       })
    //     } else {
    //       this.setData({
    //         resultHouse: {}
    //       })
    //     }
    //   }
    // })
    this.setData({
      // historyList: historyList,
      allHouseList: allList
    })
  },
  //搜索回调
  searchAction(e) {
    // console.log("JJLog:搜索回调:" + JSON.stringify(e));
    this.setData({
      searchWord: e.detail.searchWord,
      'formData.words': e.detail.searchWord
    })
    this.queryRecommendHouse(true);

  },



  //确定选择房源:
  sureSelectAction() {
    console.log("JJLog:选中的房源是:" + JSON.stringify(this.data.resultHouse));
    if (JSON.stringify(this.data.resultHouse) == "{}") {
      wx.showToast({
        title: '请选择要报备的楼盘',
        icon: "none"
      })
      return;
    }
    app.globalData.reportHouse = this.data.resultHouse;
    wx.navigateBack({
      delta: 0,
    })
  },

  //搜索结果点击:
  searchItemAction(e) {
    let index = e.currentTarget.dataset.index;
    let item = this.data.realSearchList[index];
    // console.log("JJLog:点击的item:" + JSON.stringify(item));
    app.globalData.reportHouse = item;
    wx.navigateBack({
      delta: 0,
    })
  },












})